var db = require('../utils/db');

class MoviesModel {

  constructor() {
    this.model = db.model('movies', {
      nm: String,
      img: String,
      version: String,
      cityId: String,
      cityName: String,
      type: String,
      star: String,
      sc: Number,
      wish: Number,
      showInfo: String,
      globalReleased: Boolean,
      showTime: String,
      createTime:String
    })
  }


  save(data) {
    let moviesModel = new this.model(data);
    return moviesModel.save()
  }

  /**
   * 职位列表查询
   * @param {*} searchParams 查询条件
   * @param {*} pagesize 每页条数
   * @param {*} pageno 第几页
   * @param {*} sortname 排序字段
   * @param {*} sort asc | desc
   */
  query(searchParams, pagesize, pageno, sortname, sort) {
    console.log(searchParams, pagesize, pageno, sortname, sort)
    return this.model.find(searchParams)
      .limit(pagesize * 1)
      .skip((pageno) * pagesize)
      .sort({ [sortname]: sort === 'asc' ? 1 : -1 })


  }

  count(params) {
    // let reg = new RegExp(keyword, 'i');
    // return this.model.find({ $or: [{ companyName: reg }, { positionName: reg }] }).count()
    return this.model.find(params).count()
  }

  findone(_id) {
    return this.model.findOne({ _id })
  }

  update(_id, data) {
    console.log(_id, data)
    return this.model.updateOne({ _id }, { $set: data })
  }

  remove(_ids) {
    console.log(_ids)
    return this.model.deleteMany({ _id: { $in: _ids } })
  }

}

module.exports = new MoviesModel();